package com.hethongviet.job.helpers.listadapter;

import java.util.List;

import com.hethongviet.job.Config;
import com.hethongviet.job.R;
import com.hethongviet.job.models.Comment;

import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListItemCommentAdapter extends ArrayAdapter<Comment>
{
	private final List<Comment> values;
	private final Activity activity;

	/* Make higher performance */
	private static class ViewHolder
	{
		public TextView _textCommentContent;
		public TextView _textCommentSender;
		public TextView _textCommentDateCreate;
		public ImageView _imageViewUnread;
	}

	public ListItemCommentAdapter(Activity activity, List<Comment> values)
	{
		super(activity, R.layout.list_item_comment, values);
		this.values = values;
		this.activity = activity;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		View rowView = convertView;
		final ViewHolder viewHolder;

		if (rowView == null)
		{
			LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			rowView = layoutInflater.inflate(R.layout.list_item_comment,
					parent, false);
			viewHolder = new ViewHolder();
			viewHolder._textCommentContent = (TextView) rowView.findViewById(R.id.textCommentContent);
			viewHolder._textCommentSender = (TextView) rowView.findViewById(R.id.textCommentSender);
			viewHolder._textCommentDateCreate = (TextView) rowView.findViewById(R.id.textCommentDateCreate);
			viewHolder._imageViewUnread = (ImageView) rowView.findViewById(R.id.imageUnread);
			rowView.setTag(viewHolder);
		}
		else
		{
			viewHolder = (ViewHolder) rowView.getTag();
		}
		/* Initiate items */
		final Comment currentComment = values.get(position);
		String contentString = currentComment.getContent();
		if (contentString != null && contentString.length() > 20) viewHolder._textCommentContent.setText((contentString).substring(
				0, 20) + "...");
		else viewHolder._textCommentContent.setText(contentString);

		if (Config.USE_COMMENT_MARK_UNREAD)
		{
			if (currentComment.isRead())
			{
				viewHolder._imageViewUnread.setVisibility(View.GONE);
				viewHolder._textCommentContent.setText(Html.fromHtml("<font color=\"black\">"
						+ viewHolder._textCommentContent.getText().toString()
						+ "</font>"));
			}
			else
			{
				viewHolder._imageViewUnread.setVisibility(View.VISIBLE);
				// viewHolder._textCommentContent.setText(Html.fromHtml("<B>"
				// + viewHolder._textCommentContent.getText().toString()
				// + "</B>"));
			}
		}
		else viewHolder._imageViewUnread.setVisibility(View.GONE);

		viewHolder._textCommentSender.setText(currentComment.getSender());
		viewHolder._textCommentDateCreate.setText(currentComment.getDateCreate());

		/* Return view */
		return rowView;
	}
}
